﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Mystery.SharePoint
{
	public static class ProvisioningUtilities
	{
		/// <summary>
		/// Creates an object based on a string
		/// </summary>
		/// <typeparam name="T">The type the return object should be cast to</typeparam>
		/// <param name="fullName">Full name of object to create</param>
		/// <returns>A reference to the object, or null if not as the desired type specified</returns>
		public static T CreateObjectFromString<T>(string fullName) where T : class
		{
			if (string.IsNullOrEmpty(fullName) == true) throw new ArgumentNullException("fullName");

			int comma = fullName.IndexOf(',');
			string className = fullName.Substring(0, comma);
			string assemblyName = fullName.Substring(comma + 1);

			Assembly assembly = Assembly.Load(assemblyName);
			Type type = assembly.GetType(className, true, false);

			return Activator.CreateInstance(type) as T;
		}
		/// <summary>
		/// Creates an object based on a string
		/// </summary>
		/// <typeparam name="T">The type the return object should be cast to</typeparam>
		/// <param name="fullName">Full name of object to create</param>
		/// <param name="args">A set of values to pass to the constructor</param>
		/// <returns>A reference to the object, or null if not as the desired type specified</returns>
		public static T CreateObjectFromString<T>(string fullName, params object[] args) where T : class
		{
			if (string.IsNullOrEmpty(fullName) == true) throw new ArgumentNullException("fullName");

			int comma = fullName.IndexOf(',');
			string className = fullName.Substring(0, comma);
			string assemblyName = fullName.Substring(comma + 1);

			Assembly assembly = Assembly.Load(assemblyName);
			Type type = assembly.GetType(className, true, false);

			return Activator.CreateInstance(type, args) as T;
		}
	}
}
